<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class roles_model extends CI_Model{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function getAllPermissionByRoleId($roleId){        
        $this->db->select('sy_permissions.*');
        $this->db->from('sy_permissions');
        $this->db->join('sy_role_permissions', 'sy_role_permissions.permission_id = sy_permissions.permission_id');
        $this->db->join('sy_roles', 'sy_roles.role_id = sy_role_permissions.role_id');
        $this->db->where('sy_roles.role_id',$roleId);
        $this->db->order_by("sy_permissions.permission_id", "asc");
        $this->db->order_by("sy_permissions.name", "asc");
        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;   
    }
    
    function getAllPermission(){        
        $this->db->select('*');
        $this->db->from('sy_permissions');
        $this->db->order_by("permission_id", "asc");
        $this->db->order_by("name", "asc");
        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;   
    }
    
    function loadPermission(){
        $permission = $this->getAllPermission();
        $mapping = array();
        
        foreach($permission As $item){
            $split = explode(".", $item->name);
            
            if( array_key_exists($split[0], $mapping) ){
                $mapping[$split[0]][] = array('row'=>$split[1],'col'=>$split[2],'permission_id'=>$item->permission_id,'checked'=>false);
            }else{
                $mapping[$split[0]][] = array('row'=>$split[1],'col'=>$split[2],'permission_id'=>$item->permission_id,'checked'=>false);
            }
            
        }
        return $mapping;
    }
    
    function insertRolePermission($data){
        $this->db->insert('sy_role_permissions', $data);
        //return $this->db->insert_id();
    }
    
    function deleteRolePermission($roleId, $permissionId){
        $this->db->delete('sy_role_permissions', array('role_id'=>$roleId,'permission_id' => $permissionId));
    }
    
    function insertRole($data){
        $this->db->insert('sy_roles', $data);
        return $this->db->insert_id();
    }
    
    function getAllListRoles($itemsPerPage,$start){
        $this->db->select('*');
        $this->db->from('sy_roles');
        $this->db->where('deleted',0);
        $this->db->order_by("role_id", "asc");
        $this->db->limit($itemsPerPage, $start);

        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;
    }
    
    function countAllListRoles(){
        $this->db->select('*');
        $this->db->from('sy_roles');
        $this->db->where('deleted',0);
        $this->db->order_by("role_id", "asc");

        //$query = $this->db->get();
        return $this->db->count_all_results();
    }
    
    function updateRole($id,$data){
        $this->db->where('role_id', $id);
        $this->db->update('sy_roles', $data); 
    }
    
    function getRoleById($roleId){
        $query = $this->db->get_where('sy_roles', array('role_id' => $roleId));
        if($query->num_rows() == 1) return $query->row_array();
        return false;
    }
    
    function insertPermission($data){
        $this->db->insert('sy_permissions', $data);
        return $this->db->insert_id();
    }
    
    function updatePermission($id,$data){
        $this->db->where('permission_id', $id);
        $this->db->update('sy_permissions', $data); 
    }
    
    function getPermissionById($permissionId){
        $query = $this->db->get_where('sy_permissions', array('permission_id' => $permissionId));
        if($query->num_rows() == 1) return $query->row_array();
        return false;
    }
    
    function getAllListPermission($itemsPerPage,$start){
        $this->db->select('*');
        $this->db->from('sy_permissions');
        $this->db->where('status !=',-1);
        $this->db->order_by("name", "asc");
        $this->db->order_by("permission_id", "asc");
        $this->db->limit($itemsPerPage, $start);

        $query = $this->db->get();
        
        if($query->num_rows() > 0) return $query->result();
        return false;
    }
    
    function countAllListPermission(){
        $this->db->select('*');
        $this->db->from('sy_permissions');
        $this->db->where('status !=',-1);
        $this->db->order_by("name", "asc");
        $this->db->order_by("permission_id", "asc");

        //$query = $this->db->get();
        return $this->db->count_all_results();
    }
    
}